package com.sms.plateform.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sms.plateform.common.page.PageDataLists;
import com.sms.plateform.common.page.PageProperty;
import com.sms.plateform.common.util.Utils;
import com.sms.plateform.kernel.entity.RaskQuery;
import com.sms.plateform.kernel.entity.UserInfo;
import com.sms.plateform.kernel.service.IRaskQueryService;

@Controller
public class RaskQueryController extends BaseController {
	@Autowired
	private IRaskQueryService raskQueryService;
	@SuppressWarnings("unchecked")
	@RequestMapping("/raskquery_list_detil")
	public String detilListRaskQueryHandel(HttpServletRequest request,HttpServletResponse response,ModelMap model) {
		UserInfo userinfo=this.getUserInfo(request);
		Map<String, Object> param=new HashMap<String, Object>();
		param.put("gid", userinfo.getGid());
		String batchid=this.getUrlParam(request, "batchid");
		param.put("batchid",batchid);
		List<RaskQuery> raskQueryList= this.raskQueryService.listModel(param);
		List<HashMap<String, Object>> newRaskQueryList=new ArrayList<HashMap<String, Object>>();
		for (RaskQuery raskQuery : raskQueryList) {
			String mobils[]=raskQuery.getMobile().split(",");
			for (int i = 0; i < mobils.length; i++) {
				HashMap<String, Object> hashmap=new HashMap<String, Object>();
				hashmap.put("mobile", mobils[i]);
				hashmap.put("message", raskQuery.getMessage());
				hashmap.put("channelid", raskQuery.getChannelid());
				hashmap.put("status", raskQuery.getStatus());
				hashmap.put("stype", raskQuery.getStype());
				hashmap.put("sendtime", raskQuery.getSendtime());
				hashmap.put("createtime", raskQuery.getCreatetime());
				newRaskQueryList.add(hashmap);
			}
		}
		if(newRaskQueryList.size()<1){
			model.put("raskmsg", "已全部发送完成，请在短信记录中查询。");
			return "common/raskmsg";
		}
		model.put("batchid",batchid);
		String message=Utils.trim(newRaskQueryList.get(0).get("message"));
		model.put("message",message);
		model.put("stype", newRaskQueryList.get(0).get("stype"));
		model.put("sendtime", newRaskQueryList.get(0).get("sendtime"));
		model.put("messagelen",message.length());
		model.put("raskQueryList", newRaskQueryList);
		return "rask/raskquery_list_detil";
	}
	@RequestMapping("/raskquery_list")
	public String listRaskQueryHandel(HttpServletRequest request,HttpServletResponse response,ModelMap model){
		UserInfo userInfo=this.getUserInfo(request);
		PageProperty pp = new PageProperty();
		pp.setNpage(this.getUrlParam(request, "pageNo")); // 更新页码值
		pp.setNpagesize(this.getUrlParam(request, "pageSize")); // 更新页面查询数量值
		pp.putParamMap("gid",userInfo.getGid());
		PageDataLists<RaskQuery> raskQueryPageLists= this.raskQueryService.getPageDateList(pp);
		model.put("pageDataLists", raskQueryPageLists);
		return "rask/raskquery_list";
	}
}
